package com.yls.except.util.kuaidiniao;

import com.yls.except.util.ConstantUtil;
import com.yls.except.util.GeneralResult;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map; 

/**
 *
 * 快递鸟物流轨迹即时查询接口
 *
 * @技术QQ群: 456320272
 * @see: http://www.kdniao.com/YundanChaxunAPI.aspx
 * @copyright: 深圳市快金数据技术服务有限公司
 *
 * DEMO中的电商ID与私钥仅限测试使用，正式环境请单独注册账号
 * 单日超过500单查询量，建议接入我方物流轨迹订阅推送接口
 * 
 * ID和Key请到官网申请：http://www.kdniao.com/ServiceApply.aspx
 */

public class KdniaoTrackQueryAPI {
	
	//DEMO
	public static void main(String[] args) {
		KdniaoTrackQueryAPI api = new KdniaoTrackQueryAPI();
		try {
			String result = api.getOrderTracesByJson("HTKY", "811279018204");
			System.out.print(result);
			Map<String, Object> map = GeneralResult.jsonStrToMap(result);
			System.out.println(map.get("EBusinessID"));
			System.out.println(map.get("ShipperCode"));
			System.out.println(map.get("Success"));
			System.out.println(map.get("LogisticCode"));
			System.out.println(map.get("State"));
			JSONArray jsonArray = (JSONArray) map.get("Traces");
			for (int i = 0; i < jsonArray.length(); i ++) {
			    JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                System.out.println("AcceptTime"+i+":" + jsonObject.get("AcceptTime"));
                System.out.println("AcceptStation"+i+":" + jsonObject.get("AcceptStation"));
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//请求url
	private static String ReqURL="http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx";

    /**
     *
     * Json方式 查询订单物流轨迹
     * @param expCode 快递公司编号
     * @param expNo 物流单号
     * @return
     */
	public static String getOrderTracesByJson(String expCode, String expNo){
		String requestData= "{'OrderCode':'','ShipperCode':'" + expCode + "','LogisticCode':'" + expNo + "'}";
		
		Map<String, String> params = new HashMap<String, String>();
        try {
            params.put("RequestData", KdApiCreateOrderDemo.urlEncoder(requestData, "UTF-8"));
            params.put("EBusinessID", ConstantUtil.EBUSINESSID);
            params.put("RequestType", "1002");
            String dataSign=KdApiCreateOrderDemo.encrypt(requestData, ConstantUtil.APPKEY, "UTF-8");
            params.put("DataSign", KdApiCreateOrderDemo.urlEncoder(dataSign, "UTF-8"));
            params.put("DataType", "2");

            String result=KdApiCreateOrderDemo.sendPost(ReqURL, params);
            //根据公司业务处理返回的信息......

            return result;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
